package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.MyTree;
import com.ruoyi.common.core.domain.Ztree;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ApplyTable;
import com.ruoyi.system.service.IApplyTableService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;

/**
 * 表格管理Controller
 * 
 * @author ruoyi
 * @date 2021-11-13
 */
@Controller
@RequestMapping("/table/table")
public class ApplyTableController extends BaseController
{
    private String prefix = "table/table";

    @Autowired
    private IApplyTableService applyTableService;

    @RequiresPermissions("table:table:view")
    @GetMapping()
    public String table()
    {
        return prefix + "/table";
    }

    /**
     * 查询表格管理树列表
     */
    @RequiresPermissions("table:table:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ApplyTable> list(ApplyTable applyTable)
    {
        List<ApplyTable> list = applyTableService.selectApplyTableList(applyTable);
        return list;
    }

    /**
     * 导出表格管理列表
     */
    @RequiresPermissions("table:table:export")
    @Log(title = "表格管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ApplyTable applyTable)
    {
        List<ApplyTable> list = applyTableService.selectApplyTableList(applyTable);
        ExcelUtil<ApplyTable> util = new ExcelUtil<ApplyTable>(ApplyTable.class);
        return util.exportExcel(list, "表格管理数据");
    }

    /**
     * 新增表格管理
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("applyTable", applyTableService.selectApplyTableById(id));
        }else{
            ApplyTable applyTable = new ApplyTable();
            applyTable.setId(0L);
            mmap.put("applyTable",applyTable);
        }
        return prefix + "/add";
    }

    /**
     * 新增保存表格管理
     */
    @RequiresPermissions("table:table:add")
    @Log(title = "表格管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ApplyTable applyTable)
    {
        return toAjax(applyTableService.insertApplyTable(applyTable));
    }

    /**
     * 修改表格管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ApplyTable applyTable = applyTableService.selectApplyTableById(id);
        mmap.put("applyTable", applyTable);
        return prefix + "/edit";
    }

    /**
     * 修改保存表格管理
     */
    @RequiresPermissions("table:table:edit")
    @Log(title = "表格管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ApplyTable applyTable)
    {
        return toAjax(applyTableService.updateApplyTable(applyTable));
    }

    /**
     * 删除
     */
    @RequiresPermissions("table:table:remove")
    @Log(title = "表格管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        if(id == 6L){
            return error("表格节点不允许删除");
        }
        ApplyTable applyTable = new ApplyTable();
        applyTable.setParentid(Long.toString(id));
        List<ApplyTable> x =applyTableService.selectApplyTableList(applyTable);
        if( x.size() != 0) {
            return error("存在子节点不允许删除");
        }
        return toAjax(applyTableService.deleteApplyTableById(id));
    }

    /**
     * 选择表格管理树
     */
    @GetMapping(value = { "/selectTableTree/{id}", "/selectTableTree/" })
    public String selectTableTree(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("applyTable", applyTableService.selectApplyTableById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 加载表格管理树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = applyTableService.selectApplyTableTree();

        return ztrees;
    }

    /**
     * 加载表格管理树列表
     */
    @GetMapping("/treeData2")
    @ResponseBody
    public List<MyTree> treeData2()
    {
        List<MyTree> ztrees = applyTableService.selectApplyTableMyTree(new ApplyTable());

        ztrees = MyTree.CombinationTree(0L,ztrees);

        return ztrees;
    }
}
